﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using Microsoft.EntityFrameworkCore;
using OplugAbpProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CppServices
{
    public class T_Biz_CarrierAppService : AsyncCrudAppService<T_Biz_Carrier, T_Biz_CarrierDto, Guid, PagedResultRequestDto, UpdateT_Biz_CarrierDto, UpdateT_Biz_CarrierDto>, IT_Biz_CarrierAppService
    {
        private readonly IObjectMapper _objectMapper;
        public T_Biz_CarrierAppService(IRepository<T_Biz_Carrier, Guid> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }

        #region 物流服务商管理
        /// <summary>
        /// 物流服务商管理
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="cCarrierCode">物流编码</param>
        /// <param name="cName">承运商名称</param>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<T_Biz_CarrierDto>> GetList(string nCompanyID, string cCarrierCode = "", string cName = "", int skipCount = 0, int maxResultCount = 10)
        {
            PagedResultDto<T_Biz_CarrierDto> resultDto = new PagedResultDto<T_Biz_CarrierDto>();
            List<T_Biz_Carrier> lists = new List<T_Biz_Carrier>();
            Expression<Func<T_Biz_Carrier, bool>> expr = o => o.NCompanyID == nCompanyID;
            if (!string.IsNullOrEmpty(cCarrierCode))//物流编码
            {
                expr = expr.ExpressionAnd(o => o.CCarrierCode == cCarrierCode);
            }
            if (!string.IsNullOrEmpty(cName))//承运商名称
            {
                expr = expr.ExpressionAnd(o => o.CName == cName);
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
            resultDto.Items = _objectMapper.Map<List<T_Biz_CarrierDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion
    }
}
